﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Resources;
using System.ComponentModel;
using VIBlend.WPF.Controls;
using System.Reflection;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{
    [Category("VIBlend.WPF.Examples")]
    public partial class PivotDesignDemo : Window, IWindow
    {
        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public void SetTheme()
        {
            this.dataGrid1.FontFamily = new FontFamily("Tahoma");
        }
  
        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }


        public PivotDesignDemo()
        {
            InitializeComponent();

            PrepareGridData();

            this.dataGrid1.BindingComplete += new EventHandler(dataGrid1_BindingComplete);
            this.dataGrid1.BindingStart += new EventHandler(dataGrid1_BindingStart);

            PrepareGridData();

            dataGrid1.ItemsSource = this.lst;
        }

        /// <summary>
        /// Implements a Binding Start event handler.
        /// </summary>
        void dataGrid1_BindingStart(object sender, EventArgs e)
        {
            // Set the appropriate CellDataTemplate for each BoundPivotValue depending on the Pivot Function
            // In this example we use a currrency formatting for all functions except COUNT
            foreach (BoundValueField valueField in dataGrid1.BoundPivotValues)
            {
                if (valueField.Function != PivotFieldFunction.Count && valueField.DataField == "ExtendedPrice")
                {
                    valueField.CellTextFormatString = "{0:C}"; // format as currency
                    valueField.CellStyle = this.Resources["styleContentRight"] as Style;
                }
                else
                {
                    valueField.CellTextFormatString = string.Empty;
                    valueField.CellStyle = this.Resources["styleContentCentered"] as Style;
                }
            }
        }

        /// <summary>
        /// Implements a BindingComplete event handler.
        /// Resize all rows and columns right after the binding is done.
        /// </summary>
        void dataGrid1_BindingComplete(object sender, EventArgs e)
        {
            this.dataGrid1.ColumnsHierarchy.AutoResize();
            this.dataGrid1.RowsHierarchy.AutoResize();
        }

        #region Sample Data

        /// <summary>
        /// Represents a sample Invoice class.
        /// </summary>
        public class Invoice
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="Invoice"/> class.
            /// </summary>
            /// <param name="City">The city.</param>
            /// <param name="Region">The region.</param>
            /// <param name="Country">The country.</param>
            /// <param name="SalesPerson">The sales person.</param>
            /// <param name="ShipperCompany">The shipper company.</param>
            /// <param name="ProductName">Name of the product.</param>
            /// <param name="UnitPrice">The unit price.</param>
            /// <param name="Quantity">The quantity.</param>
            /// <param name="Discount">The discount.</param>
            /// <param name="ExtendedPrice">The extended price.</param>
            /// <param name="Freight">The freight.</param>
            public Invoice(
                string City,
                string Region,
                string Country,
                string SalesPerson,
                string ShipperCompany,
                string ProductName,
                double UnitPrice,
                double Quantity,
                double Discount,
                double ExtendedPrice,
                double Freight)
            {
                this.SalesPerson = SalesPerson;
                this.City = City;
                this.Region = string.IsNullOrEmpty(Region) ? "All Regions" : Region;
                this.Country = Country;
                this.ShipperCompany = ShipperCompany;
                this.ProductName = ProductName;
                this.UnitPrice = UnitPrice;
                this.Quantity = Quantity;
                this.Discount = Discount;
                this.ExtendedPrice = ExtendedPrice;
                this.Freight = Freight;
            }

            /// <summary>
            /// Gets or sets the sales person.
            /// </summary>
            /// <value>
            /// The sales person.
            /// </value>
            public string SalesPerson { get; set; }
            /// <summary>
            /// Gets or sets the city.
            /// </summary>
            /// <value>
            /// The city.
            /// </value>
            public string City { get; set; }
            /// <summary>
            /// Gets or sets the region.
            /// </summary>
            /// <value>
            /// The region.
            /// </value>
            public string Region { get; set; }
            /// <summary>
            /// Gets or sets the country.
            /// </summary>
            /// <value>
            /// The country.
            /// </value>
            public string Country { get; set; }
            /// <summary>
            /// Gets or sets the shipper company.
            /// </summary>
            /// <value>
            /// The shipper company.
            /// </value>
            public string ShipperCompany { get; set; }
            /// <summary>
            /// Gets or sets the name of the product.
            /// </summary>
            /// <value>
            /// The name of the product.
            /// </value>
            public string ProductName { get; set; }
            /// <summary>
            /// Gets or sets the unit price.
            /// </summary>
            /// <value>
            /// The unit price.
            /// </value>
            public double UnitPrice { get; set; }
            /// <summary>
            /// Gets or sets the quantity.
            /// </summary>
            /// <value>
            /// The quantity.
            /// </value>
            public double Quantity { get; set; }
            /// <summary>
            /// Gets or sets the discount.
            /// </summary>
            /// <value>
            /// The discount.
            /// </value>
            public double Discount { get; set; }
            /// <summary>
            /// Gets or sets the extended price.
            /// </summary>
            /// <value>
            /// The extended price.
            /// </value>
            public double ExtendedPrice { get; set; }
            /// <summary>
            /// Gets or sets the freight.
            /// </summary>
            /// <value>
            /// The freight.
            /// </value>
            public double Freight { get; set; }
        }

        /// <exclude/>
        public List<Invoice> lst = new List<Invoice>();

        private void PrepareGridData()
        {
            try
            {
                StreamResourceInfo sri = Application.GetResourceStream(new Uri("ExamplesCSharp;component/CSharp/DataGridViewDemos/invoices.tab", UriKind.Relative));
                System.IO.StreamReader sr = new System.IO.StreamReader(sri.Stream);
                string line = null;
                sr.ReadLine();
                int i = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    i++;
                    string[] tabs = line.Split('\t');
                    lst.Add(new Invoice(tabs[0], tabs[1], tabs[2], tabs[3], tabs[4], tabs[5], double.Parse(tabs[6]), double.Parse(tabs[7]), double.Parse(tabs[8]), double.Parse(tabs[9]), double.Parse(tabs[10])));

                    if (i >= 500)
                        break;
                }
                sr.Close();
            }
            catch (System.Exception)
            {
            }
        }
        #endregion

        protected override void OnClosing(CancelEventArgs e)
        {
            this.lst.Clear();
            this.lst = null; 
            base.OnClosing(e);
            this.dataGrid1.Dispose();
            this.dataGrid1 = null;
        }
    }
}
